Wordpress how to create custom post template from parts of INDEX.php! On my Frontpage (Index.php) i have a great Post Grid. I would like to have this Grid also under each post (Under the Post Content between Comment field). I have copy some parts from the (Index.php) Code and inserted in the post.php but, the result was not satisfactorily.
I have searched everywhere but can not find a satisfying solution, which is compatible with my wordpress theme. If someone can help me, please let me know what code do you need. I would be more than happy to hear a solution!
Thanks in Advance
Not very clear what you mean, but I think you want to print some posts on your single.php file. To accomplished this, you'll need a WP_Query similar to:
<?php
// the query
$args = array(
'post_type' => 'post' // change as needed
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2> <!-- Copy same structure and data as the one on the index.php file -->
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
You will need to keep the same div structure and classes as the one on the index.php to make them look alike.
Good luck!
Related
The short problem: Editing the sanitize function has allowed me to add uppercase letters and symbols to my permalinks. However, when I use a symbol (like: ".", "=", "?") the link ends up in a 404 error. I'm guessing because the database can't access the page with a URL like that.
Are there any ways around this?
The long problem: I'm transitioning someone's site that was written in ASP on a windows server into a wordpress site. I've copied each page's content and created a wordpress site that duplicates the site structure of the current ASP one. But I need to keep the URL's the same, and his current site pages look like "http://thesite.com/ContentPage.aspx?page=subpage".
You can create a page hierarchy, apply a rewrite rule and create a page template, to solve the problem without having to add extra content to the permalink and the final URL would end up looking like the old ASP URL.
The query variable 'page' holds the pagenumber for a single paginated Post or Page, using this queryvar isn't recommended. You should review the need to maintain the url structure.
Taking this page structure for example:
Animals (permalink: http://thesite.com/animals/)
Lion (permalink: http://thesite.com/animals/lion)
Zebra (permalink: http://thesite.com/animals/zebra)
Others (permalink: http://thesite.com/animals/others)
Flowers (permalink: http://thesite.com/flowers/)
If I access http://thesite.com/Animals.aspx?page=Lion I should be presented the "Lion" page
For this to work you have to add to your functions file the add_rewrite_rule function with the following regex:
^([\w\d\-]*)(?:\.aspx?).*$
(I'm not used to regexp so i guess there's an easier way to write it.)
resulting in something like this:
add_rewrite_rule('^([\w\d\-]*)(?:\.aspx?).*$','index.php?pagename=$matches[1]','top');
Use it in a function like the example in the Codex. This way you'll access http://thesite.com/index.php?pagename=Animals when entering http://thesite.com/Animals.aspx?page=Lion in the address bar.
Do not forget to flush and regenerate the rewrite rules database after modifying rules.
So now you must get the 'page' queryvar and do a custom query to get the page children. The best way to do this is creating a page template. Create a file aspx-template.php in your theme folder and add the following:
<?php
/**
* Template Name: aspx Template
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$pagename = get_post_field( 'post_name', get_the_ID() );
if ( isset($_GET['page']) ) : $pagename .= '/' . sanitize_text_field( $_GET['page'] ); endif;
$args = array(
'post_type' => 'page',
'pagename' => $pagename,
'post_status' => 'publish'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<article>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<div>
No page found
</div>
<?php endif; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Don't forget to associate the template to the parent page (Animals).
I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!
I need to get some posts by category and display them out of the wordpress. I need these posts hidden in the wp blog. But visible in my custom page. Via php.
I'm noob with wordpress. Can anyone direct me about how can I do that?
Your file should be something like this:
<?php
require '/path/to/wordpress/root/wp-load.php';
$the_query = new WP_Query( 'post_status=private' );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
?>
Read more about WP_Query class to get posts which you need.
Does anyone know what the best way to implement a list/grid togglable view in wordpress would be?
To be more clear: the default view shows the posts in a list, I would like to have the option to show them in a grid.
I have so far created a loop which shows only the thumbnails, and included it in another template.
But I don't know how I would link to that view. Would I best off using an archive view?
Thanks.
One of the easiest solutions is to create a page template containing a grid view and add a link to this page in the list view. Very simple but just what you need.
I had the same issue with pagination. WordPress has built in function posts_nav_link that automatically prints links to previous and next pages if needed. The problem is that this function works only with $wp_query instance of WP_Query (this instance is used by default, for example to get page's content or the latest posts in home). So the solution for you:
<?php
$temp=$wp_query;
$wp_query=null;
$wp_query = new WP_Query('showposts=4');
while(have_posts() ) : the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile;
posts_nav_link();
$wp_query = $temp; ?>
I think that's quite obvious and there is no need of explanation :) should be working
Thanks dude. I kind of got it working for now with this:
<?php $latest = new WP_Query('showposts=4'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I think my mistake was trying to get it to use get_template_part( 'loop', 'grid' ); It would show the page, but no posts. Even if used get_template_part( 'loop', 'index' );
I'll need to figure out the pagination, but I'm putting it down for now to work on something else.
Thanks for your help so far! #Gediminas
Basically, I want to have two columns Wordpress page; one displaying video posts, and the other audio posts.
What's the easier way of doing this?
<div class="leftcol">
<?php $args = array( 'post_type' => 'video_post' ); //select only posts of type 'video_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<div class="post">
<h3><?php the_title() ?></h3>
<?php the_content() ?>
</div>
<?php endwhile; rewind_posts(); ?>
</div>
<div class="rightcol">
<?php $args = array( 'post_type' => 'audio_post' ); //select only posts of type 'audio_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; rewind_posts(); ?>
</div>
I'm assuming you do know the basics of editing a theme, but if not, just say so! Alternatively, you could do the same thing using 'cat' instead of 'post_type' to separate your posts by category instead of two different types. I'd recommend using categories if you don't intend on adding any extra fields to enable your audio/video functionality.
I'm not sure if I understood you well, but I would do it with Widgets using Widget Logic plugin http://wordpress.org/extend/plugins/widget-logic/
If you want to hardcode your template you may need to check the query_posts() and reset_posts() finctions: http://codex.wordpress.org/Function_Reference/query_posts
Do you know how to use WordPress Codex? Do you know how the loop works? If yes you may want to use the second way, if not try to do it with the first option.
Good luck,
gezope