ivandoric / WordPress-Custom-Query

Repository for Wordpress Custom Query video tutorial series
24 stars 13 forks source link

Fetching Correct Permalink #2

Closed Moniet closed 7 years ago

Moniet commented 7 years ago

After changing the loop to include the query posts, the wp_list_categories(); function is fetching the wrong link for the posts. I've tried resetting the permalink to get the /%postname%/ but it didn't work.

Example By Image

screen shot 2017-06-23 at 10 56 20 am screen shot 2017-06-23 at 10 56 29 am

MY INDEX PHP

`<?php get_header(); ?>

    <div id="main">
     <div id="content">
    <!-- Featured Posts -->
    <?php     $args = array(
            'post_type' => 'post',
            'posts_per_page' => 20,
            'category_name' => 'featured',
            'order' => 'ASC',  
        );
        $query = new WP_Query($args);
        while($query->have_posts()) : $query->the_post();
    ?>
    <!-- The posts for this section -->
       <div class="post_container_featured">
            <div class="post_featured">
                <a href="<?php the_permalink(); ?>">
                <div class="excerpt_featured"><?php the_excerpt(); ?></div>
                </a>
            </div>
        </div>
    <?php endwhile; wp_reset_query();  ?>
    <!-- Section | Do You Really Want An Elite Graduate Job  -->
    <hr>
    <h4>Do You Really Want An Elite Graduate Job?</h4>
    <?php
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 20,
            'category_name' => 'do-you-really-want-an-elite-graduate-job',
            'order' => 'ASC',  
        );

        $query = new WP_Query($args);

        while($query->have_posts()) : $query->the_post();
    ?>
    <!-- The posts for this section -->
       <div class="post_container">
            <div class="posts">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_content(); ?></p>
                <div class='clearing'></div>
            </div>
            <div class="post-footer"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div> 
    <?php endwhile; wp_reset_query();  ?>
    <!-- Section | Realities of the Elite Graduate Job Market  -->
    <hr>
    <h4>Realities of the Elite Graduate Job Market</h4>
    <?php
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 20,
            'category_name' => 'realities-of-the-elite-graduate-job-market',
            'order' => 'ASC',  
        );

        $query = new WP_Query($args);

        while($query->have_posts()) : $query->the_post();
    ?>
    <!-- The posts for Realities of the Elite Graduate Job Market section -->
       <div class="post_container">
            <div class="posts">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_content(); ?></p>
                <div class='clearing'></div>
            </div>
            <div class="post-footer"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div> 
    <?php endwhile; wp_reset_query();  ?>
    <!-- Section | Types of Elite Graduate Careers  -->
    <hr>
    <h4>Types of Elite Graduate Careers</h4>
    <?php
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 20,
            'category_name' => 'types-of-elite-careers',
            'order' => 'ASC',  
        );

        $query = new WP_Query($args);

        while($query->have_posts()) : $query->the_post();
    ?>
    <!-- The posts for this Types of Elite Graduate Careers -->
       <div class="post_container">
            <div class="posts">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_content(); ?></p>
                <div class='clearing'></div>
            </div>
            <div class="post-footer"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div> 
    <?php endwhile; wp_reset_query();  ?>
     <!-- Section | developing-an-action-plan-and-taking-control -->
    <hr>
    <h4>Developing an Action Plan and Taking Control</h4>
    <?php
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 20,
            'cat' => '33',
            'order' => 'ASC',  
        );

        $query = new WP_Query($args);

        while($query->have_posts()) : $query->the_post();
    ?>
    <!-- The posts for this Types of developing-an-action-plan-and-taking-control -->
       <div class="post_container">
            <div class="posts">
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_content(); ?></p>
                <div class='clearing'></div>
            </div>
            <div class="post-footer"><a href="<?php the_permalink(); ?>">Read More</a></div>
        </div> 
    <?php endwhile; wp_reset_query();  ?>    
      </div>
  </div>       

<?php get_footer(); ?>`

ivandoric commented 7 years ago

Can you isolate the code that is making the problem. Also in the images you added -- what should permalinks look like. Which picture shows incorrect URLs?

Moniet commented 7 years ago

Sorry if the code is very scattered. I'm using the wp_list_categories( ); function in the to get the list of categories for my sidebar. But it's getting the wrong link. Look at the bottom left corner of the images where I've highlighted the links in the sidebar (which you get with the wp_list_categories) as well as the links within the post (which you get with the the_permalink(); function). This code is in the header.php file.

ivandoric commented 7 years ago

What happens when you click on the links? Do you get 404 error? Also Why are you using wp_list_categories to list posts. It's used for listing categories. Also I don't see that anywhere in your code. Can you paste that bit of code here.

Moniet commented 7 years ago

The header.php File that contains the wp_list_categories(); function You can find this function under the under the <ul id=sidebar> tag.

<html>
<head>
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id="header">
  <object data="<?php echo get_stylesheet_directory_uri(); ?>/img/logo.svg" id="h1" type="image/svg+xml">
  </object>
</div>
<div class="navbar">
  <section>
    <h3>Chapters</h3>
    <ul id="sidebar">
      <?php wp_list_categories('show_count=0&title_li=&hide_empty=0&exclude=1'); ?>
    </ul>
  </section>
</div>
<div class="home_user_buttons">
        <a class="home_user_link" href="/">
            <object data="<?php echo get_stylesheet_directory_uri(); ?>/img/home.svg" type="image/svg+xml" class="home_svg">
                <img class="home_img" src="<?php echo get_stylesheet_directory_uri(); ?>/img/home.png" type="image/png">
            </object>
        </a>
        <a class="home_user_link" href="/wp-login.php?">
            <object data="<?php echo get_stylesheet_directory_uri(); ?>/img/user.svg" type="image/svg+xml" class="user_svg">
                <img class="user_img" src="<?php echo get_stylesheet_directory_uri(); ?>/img/user.png" type="image/png">
            </object>
        </a>
</div>
<svg id="menu" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" height="64" viewBox="0 0 64 64.000002" width="64" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
 <metadata id="metadata7">
  <rdf:RDF>
   <cc:Work rdf:about="">
    <dc:format>image/svg+xml</dc:format> 
    <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
    <dc:title/>
   </cc:Work>
  </rdf:RDF>
 </metadata>
 <g stroke-width="10">
  <rect id="rect4243" opacity="0.99" ry="4.339" height="9.7222" width="49.629" y="27.639" x="7.1855"/>
  <rect id="rect6003" opacity="0.99" ry="4.339" height="9.7222" width="49.629" y="15" x="7.1855"/>
  <rect id="rect6005" opacity="0.99" ry="4.339" height="9.7222" width="49.629" y="40.278" x="7.1855"/>
 </g>
</svg>
<svg id="cross" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="64" viewBox="0 0 64 64.000002" width="64" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
 <metadata id="metadata7">
  <rdf:RDF>
   <cc:Work rdf:about="">
    <dc:format>image/svg+xml</dc:format>
    <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
    <dc:title/>
   </cc:Work>
  </rdf:RDF>
 </metadata>
 <rect id="rect6003" opacity="0.99" transform="matrix(.70407 -.71013 .71013 .70407 0 0)" ry="4.339" height="9.7222" width="49.629" y="40.452" x="-24.257" stroke-width="10"/>
 <rect id="rect4144" opacity="0.99" transform="matrix(-.71013 -.70407 .70407 -.71013 0 0)" ry="4.339" height="9.7222" width="49.629" y="-4.3034" x="-70.128" stroke-width="10"/>
</svg>
<div class="before_footer_block"></div>
<div class="jumbotron_container">
  <h2>Welcome!</h2>
  <p>You found us! Scroll to learn about how you can get an EliteGradJob</p>
</div>
ivandoric commented 7 years ago

Seems something went wrong when pasting the code :) check out your post, and try again :) .

Moniet commented 7 years ago

My bad, I forgot to add the markup for syntax highlighting. Anyways. When I click the links it gives 404 error because the links are wrong. For some reason the post name is giving example.com/category/post-slug value. When the actual permalink of the post is example.com/post-slug.

Could this be because I'm using the /%postname%/ function in the permalink settings on Wordpress to get the link?

Moniet commented 7 years ago

Should I not be using the wp_list_categories(); function?

ivandoric commented 7 years ago

wp_list_categories(); is used for displaying categories not posts. So that may be a problem. Try setting permalinks to be the "Default" and see if that changes things.

Moniet commented 7 years ago

I just used the get_posts(); function in the functions.php file and called it in the sidebar.php file. It's working fine now.

I think when I changed the loop to include query posts, the wp_list_cats(); function doesn't seem to work as it did with the original loop.

Moniet commented 7 years ago

Thanks for pointing out the error Ivan! I appreciate your time.