I have two Wordpress blogs on the same domain/server - one regular posts the other videos - plus other non-Wordpress pages which have a box with the latest blog posts using this code:
<ul>
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');
$args = array(
'cat' => 5, // Only source posts from a specific category
'posts_per_page' => 1 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<li>
<span class="post_title"><?php the_title(); ?></span>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ) { ?>
<span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
<?php } ?>
</a>
<span class="post_excerpt"><?php the_excerpt(); ?></span>
</li>
<? }
}
wp_reset_postdata();
?>
</ul>
All works as I want it. However we now want a second box under the first on the same pages with posts from the other blog (video). As wp-load.php has already been included for the first blog box it gets posts from the wrong blog.
How can I swap the database connection to the settings for the other (video) blog?
I have searched for solutions and found
$wpdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
I am lost as to how to use this and make it work.
I have considered merging them in to one blog but that seems a bit excessive for what would seem a simple problem so would rather not do that.
Thanks
Related
This could be a dumb question, but I just can't figure it out. I'm working on a custom WordPress theme I created by converting a html website. But when I use WP_Query, it doesn't load my blog posts. Yes, it displays a list of blog posts as it should with the loop, but when I click on each title or the 'read more' link of a post, it only changes the url in the address bar to the link of the post and then simply refreshes the hope page. It doesn't open I'm really tired. Here's the query loop in my index.php
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
//define args for query
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '30',
);
//The main query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt(); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div><!-- blog-main-content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Please what do I do
Based on your comment above, you are missing a template file for your posts page. In your current configuration, the posts loop will be displayed on all pages of your website since you only have an index.php file. You should create a single.php template to display your single blog posts.
Here is the WordPress template hierarchy for reference: https://developer.wordpress.org/themes/basics/template-hierarchy/
I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...
I am looking to feed a custom post type onto a custom template. I basically want a few sub sections with custom (seperate) post types on the backend panel feed out onto a page, like a news, blog, testimonials kind of thing.
I think have the right idea but I believe I am going around this wrong. Please forgive me, as I do not normally work on wordpess. I basically want (let's say the news page) to feed all the posts in the custom post type "news". I have a blog page linked to a template that looks like so -
<?php
/*
Template Name:News*/
?>
<?php get_header(); ?>
<ul id="News">
<?php global $post; query_posts( 'post_type=newst&orderby=ID&order=desc' ); while (have_posts()) : the_post(); ?>
<li>
<div class="fea_del">
<h1><?php the_title(); ?></h1>
<p><?php the_field('post_content',$post->ID); ?></p>
<a <?php $p=get_permalink( $post->ID ); ?> href="<?php echo $p; ?>"class="entire_job">SEE ENTIRE ARTICLE</a>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php get_footer(); ?>
I have the [display-posts] plugin in my blog page and just have [display-posts] in the body in hopes it would just feed in all the posts in blog. I've been banging my head against this for a while with no success, I don't work in wordpress much so I'm a bit in the dark here.
You have to pass the $args array to WP_Query, try:
$args = array( 'post_type' => 'news', 'order' => 'desc' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
//rest of you code
endwhile;
How can I show either a small thumbnail of a image or featured image, from each post In my recent posts custom widget
Here's my code:
<?php
include($_SERVER['DOCUMENT_ROOT'] . $root . 'blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3
));
?>
<h3 class="divider">Recent Blog Posts</h3>
<ul class="listing">
<?php foreach($recent_posts as $post) { ?>
<li><a href="<?php echo get_permalink($post['ID']) ?>">
<div><?php echo $post['post_title'] ?></div></a></li>
<?php } ?>
</ul>
You can use get_the_post_thumbnail
Usage:
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
Integrated with your code:
<?php foreach($recent_posts as $post) : ?>
<li>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<div><?php echo $post['post_title'] ?></div>
</a>
</li>
<?php endforeach; ?>
Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.
Source(s)
Function Reference/get the post thumbnail
See also
Post Thumbnails
Function Reference/add theme support
In your current installed theme, if you are making any changes to the PHP files, it will get replaced if there are any updates done to the theme. To avoid, this, create a child theme and make updates to those php files.
Thr below blog also explains in details on how to add Thumbnail to Recent post widget.
Blog on how to Add Thumbnail to Recent posts
Hope this helps.
The problem I have is I cannot get Wordpress to show the posts where I need to.
I have already tried using two methods the first in the reading settings by selecting a page to display the posts which did not work. The second was a getpost plugin which displays the post using short code [get_posts] it didn't display any post just the short code and the plugin is active.
I want it to appear like this http://www.completesource.co.uk/category/ironkey-id-theft/ but on this page http://beta.completesource.co.uk/it-news/
I have been searching for this for a while any help would be greatly appreciated.
Thank you
EDIT: code
<?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
$cat = get_cat_ID($category);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = -1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'category__in' => array($cat),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'caller_get_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_excerpt('Read the rest of this entry »'); ?>
Read More..
<BR><BR>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif;
$wp_query = $temp; //reset back to original query
} // if ($category)
?>
Personally I would write a page template for the page, then create a new page and use the template. Start reading about Page Templates and go from there.
Every Wordpress theme on the market will handle the display of posts very differently. It can very by page, shortcode, template, settings, etc.
We need to know what theme you are using in order to help you. In order to help yourself, investigate how to activate the blog by checking your themes document files or the theme developer or any help forums for the theme.