Create a Custom WordPress Page with Post titles - php

I am trying to create a custom WordPress page that will contain only the links to all my post titles, divided on 4 column. I am also using Bootstrap with WordPress.
I created the php file, created a new page with her page atribute, but the post titles don't display.
This is the code i used:
<?php
/**
* The template used for displaying page content in questions.php
*
* #package fellasladies
*/
?>
<?php
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4 col-sm-4 pbox'); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'fellasladies' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php edit_post_link( __( 'Edit', 'fellasladies' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>
</article><!-- #post-## -->
I really appreciate your help! Thanks

You'll need to start by creating a Query which fills an array with the posts you want to iterate. Read about the get_posts() function in WordPress.
Here's an example. Note that we can't use functions which are meant to be used "in the loop" such as the_title() or the_content(). We must specify the post_id for each iteration. We should not modify the main query for situations such as this.
// the arguments for the get_posts() function
$args = array(
'post_type' => 'post', // get posts int he "post" post_type
'posts_per_page' => -1 // this means the array will be filled with all posts
);
$my_posts = get_posts($args);
// now we'll iterate the posts
foreach ( $my_posts as $p ) {
// a title
echo get_the_title($p->ID);
// the link
echo get_permalink($p->ID);
// a custom field value
echo get_post_meta($p->ID,'custom_field_key',true);
}
Theming what happens inside each iteration is up to you.
Good luck! :)

I encourage you to go read about Page Templates on Wordpress Codex, that could help you a lot!
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
It seems that you have a <?php useless. You also don't define your template's name, which is required.

Related

How to add posts with navigation in home.php?

I have a theme I'm building and I'm trying to work on the homepage from home.php. I already have my listing of latest articles generating, but I want the page navigation to show up at the bottom for the next and/or previous set of articles. Ideally, some sort of lazy load too, but at the moment, I just want to understand how to do the page navigation part.
I'm having trouble finding articles on how to do this without "add THIS plugin" or "use THIS theme".
Here's what I already have to generate my articles section:
<section id="articles" class="container">
<?php
$grid_posts = get_posts( array(
'posts_per_page' => 12
) );
if ( $grid_posts ) {
foreach ( $grid_posts as $post ) :
setup_postdata( $post ); ?>
<?php
if ( has_post_thumbnail() ) : get_template_part('template-parts/posts-grid', 'image');
else : get_template_part('template-parts/posts-grid', 'default');
endif;
?>
<?php
endforeach;
wp_reset_postdata();
}
?>
</section>
I just want to find a resource that shows me how to either "load more articles" on that page or show the "next/previous page" navigation.
Thanks
You're looking for wp_link_pages
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'test' ),
'after' => '</div>',
) );
For those kind of questions and to get a basic understanding, it's also always worth at least looking into a "template theme" like _s
In your case, the file to look at would be this one

wp_query pagination on archive.php

I'm using the following wp_query on a few pages in Wordpress, and as you can see I am trying to ensure the query is paginated. Everything is working successfully on a custom page template page-articles.php, however I'm unable to get the same results on the archive.php template.
The pagination link renders successfully (e.g mydomain.com/category/my-cat/page/2) however upon clicking the link does not work, it just throws a 404 error? How can these links not go anywhere?
I'm assuming there is some issue with using custom wp_query on the archive.php template?
Thanks!
Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC'
);
$articles = new WP_Query($args );
?>
Loop
<?php if ( $articles->have_posts() ) : ?>
<?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Pagination
<nav>
<div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages ); ?></div>
<div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>
After a bit of digging around, and with help from this article, the below solution solves the issue. Just place this in your functions.php file and thats it. The below implementation works for archives of custom post types, as well as categories.
/**
* Wordpress has a known bug with the posts_per_page value and overriding it using
* query_posts. The result is that although the number of allowed posts_per_page
* is abided by on the first page, subsequent pages give a 404 error and act as if
* there are no more custom post type posts to show and thus gives a 404 error.
*
* This fix is a nicer alternative to setting the blog pages show at most value in the
* WP Admin reading options screen to a low value like 1.
*
*/
function custom_posts_per_page( $query ) {
if ( $query->is_archive('cpt_name') || $query->is_category() ) {
set_query_var('posts_per_page', 1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Yes, it is.
It is because main query of archive.php is kept unchanged while you are playing with WP_QUERY. Try use query_posts() in archive.php.
query_posts($args);
and then default loop (instead of $articles)
<?php if ( have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

ACF: Display Fields in a template based on Cpt ui

I have created a custom post type based on CPT Ui I am tryin to display the items of that in a template ideally what I want to be able to do is
www.siteurl.com/profile/profilename
Hence why I have created a template page with the following code.
But I dont no how I would get profilename into my query posts and then display the relivent data this is what I have tired so far to get id and permalink to display but to no avail.
I really want to be able to do it like how the documentation states. Ideally i would suspect my best approach would be to check if user has created a profile in the front end if not direct them to do so?.
Display a field
<p><?php the_field('field_name'); ?></p>
http://www.advancedcustomfields.com/resources/code-examples/
<?php
/**
* Template Name: Profile Page
*/
get_header(); ?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => 'david'
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo '<li>' . get_the_title($post->ID) . '</li>';
}
echo '</ul>';
}
?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If anyone has done anything simlar maybe I should be using current user but again i dont no how to link that up to acf as I am new to that plugin
Perhaps you could try another approach and build a custom query with WP_Query instead. The example bellow use the WP_Query object rather than the get_posts function, however the arguments and logic remain the same.
This implies we will find all posts that have a post_type of profiles where the ’custom field’ profilename is equal to david. The custom field ‘profilename’ in this case could be a text field, a radio button or a select field (something that saves a single text value).
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h1>__('Listing Posts by:', 'text_domain');</h1>
<?php echo '<p class="text-center">' . esc_html( $user ) . '</p>'?>;
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => $user // retrieve the actual profilename field value
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
The code above is an example of how to query ("get and display") posts in WP using WP_Query object within a template based on custom field called ‘profilename’ which has a value of ‘david’ or whatever data is saved inside the field as per your example.
Perhaps you want to display the relevant data (values) of the custom fields you've created for the WP default user profile page, and if this is the case, you should follow another approach and instead of query posts you simply output the fields a user might fill on their profile. In order to that you could Get values from a user.
This example will display the field 'profilename' value from a user with an ID of 1.
<?php the_field('field_name', 'user_1'); ?>
Within your loop you could use $user_ID = get_current_user_id(); to retrieve the user_id as follows:
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
$user_ID = get_current_user_id();
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h5>__('Listing Posts by:', 'text_domain');</h5>
<h1><?php the_field('profilename', '$user_ID'); ?>;</h1> // this will retrieve the data/value saved in the 'profilename' field for the current user
...
Check ACF's Query Posts by Custom Fields documentation for other ways to retrieve an array of post objects from the database using native WP functions.

Custom Posts not showing in archive page

I currently have a custom post type of Episode with a taxonomy of podcast. When i run the following loop on my archive page i am seeing that I have 149 posts
<?php
$args2 = array(
'posts_per_page' => 1000,
'post_type' => 'episode',
'podcast' => 'my-episodes',
'post_status' => 'publish'
);
$posts = get_posts($args2);
$count = count($posts);
echo $count;
?>
However, when I'm running this variation of the loop on my archive.php file (copied from the wordpress.org page), i am only getting 130 posts
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The loop is saying that my most recent entry was from the 5th, however i have been creating posts on a regular basis over the last few days. Are there any tests I can run to see why the most recent posts are not showing up on the archive page?
Your code appears to be correct. Double check that the posts that are missing are published and not set as a draft/saved. Also check that your custom taxonomy and post types are correct for the query you are running.
After reviewing the dashboard of the site per the recommendation of Tom here is what I was able to find
The site has a plugin called WPML activated. This allows the site to create content in multiple languages.
When the plugin is initially activated, it will ask for the site's native language and apply that to the existing posts and custom posts. This way if you were on an englsih version of the site you would see content ABC while on a spanish version you would see content XYZ.
There is a separate section inside of a custom post that allows you to make said custom post translatable. If this is not checked then your custom post will not display on any version of the site. The reason being (at least in my 5 minutes of research) is that because there is no language specified, it will not load on the language specific versions of the site
I have done two things to fix this:
I have added the ability to make the custom post translatable,
There is a setting that will allow you to display all content that is not translated on any version of the site as opposed to not displaying any content
Thank you everyone for your help.

Need help using different 'page-templates' for one-page wordpress site sections

This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>

Categories